<?php class category extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function truncate(){
    	return $this->db->truncate('category'); 
    }

    function select_join(){
    	$this->db->select('send.sendtime as sendtime,send.company_id,category.level as level,receive.receivetime as receivetime, company.name as c_name,receive.codes AS r_code, receive.count AS r_count, mod.name AS mod_name, category.name AS category_name, version.name AS version_name,send.count as s_count,send.codes as s_code,send.mod_id,send.category_id,send.version_id');
    	$this->db->from('category');
    	$this->db->join('mod','mod.mod_id = send.mod_id','left');
    	$this->db->join('company','company.company_id = send.company_id','left');
    	$this->db->join('category','category.category_id = send.category_id','left');
    	$this->db->join('version','version.version_id = send.version_id','left');
    	$this->db->join('receive','send.version_id = receive.version_id and send.mod_id = receive.mod_id and send.category_id = receive.category_id','left');
    	$query = $this->db->get();
    	$result = $query->result();
    	return $result;	   
    }
    
    function insert_for_excel($array){
       $this->name = $array['name'];
       $this->codes = $array['codes'];
       $this->mod_id = $array['mod_id'];
       $this->level = $array['level'];
       $this->updatetime = $array['updatetime'];
       $this->createtime = time();
       $re = $this->select(array('mod_id'=>$array['mod_id'],'name'=>$array['name']));
       if(count($re)==0){
         $this->db->insert('category',$this);
         $result = $this->db->insert_id();
       }else{
         $result = $re[0]->category_id;
       }
       return $result;
    }
    
    function insert(){
       $this->name = $_POST['name'];
       $this->codes = $_POST['codes'];
       $this->mod_id = $_POST['mod_id'];
       $this->level = $_POST['level'];
       $this->createtime = time();
       $re = $this->select(array('mod_id'=>$_POST['mod_id'],'name'=>trim($_POST['name'])));
       if(count($re)==0){
         $result = $this->db->insert('category',$this);
       }else{
         $result=true;
       }
       return $result;
    }
    
     function insert_array($array){

       $re = $this->select(array('mod_id'=>$array['mod_id'],'name'=>trim($array['name'])));
       if(count($re)==0){
         $result = $this->db->insert('category',$array);
       }else{
         $result=true;
       }
       return $result;
    }
    
    
    function auth($codes,$mod,$category_id){
    	$this->db->where(array('codes'=>trim($codes),'mod_id'=>$_POST['mod_id']));
    	    	
    	$sql = "select count(*) as count from category where codes=".$codes." and mod_id=".$mod;
        if($category_id!=''){
    	   $sql.= " and category_id != ".$category_id;
    	}
    	$query = $this->db->query($sql);
        $result = $query->result();
        return $result[0]->count;
    }
    
    
    function getcategory_bymod_id($mod_id){
        $this->db->where(array('mod_id'=>$mod_id));
        $this->db->order_by('codes','asc');
    	$query = $this->db->get('category');
        $result = $query->result(); 
        return $result;
    }
    
    
    function main_select_count($da,$type){
       if($type=="mod_id"||$type=="category_id"){	
            $sql = "select DISTINCT $type from category where ".$da ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function main_select($array,$num="",$offset=""){

       $sql = "select * from category left join version on version.category_id=category.category_id where ".$array ;
       $orderby = $this->session->userdata('orderby');
       if(!isset($orderby)||($orderby=="")||($orderby==0)){
                $sql.=" order by category.codes asc ";
       }else if($orderby==1){
       	        $sql.=" order by category.updatetime desc ";
       }else if($orderby==2){
       	        $sql.=" order by category.sendtime desc ";
       }
    
       $query = $this->db->query($sql);
       $result = $query->result();  $datas = array();
       
       foreach($result as $k=>$v){
       	
       	if(!in_array($v->category_id,$datas)) {
       		
          	$ar[]=(object)array(
          	                     'category_id'=>$v->category_id,
          	                     'codes'=>$v->codes,
          	                     'mod_id'=>$v->mod_id,
          	                     'level'=>$v->level,
          	                     'name'=>$v->name,
          	                     'version_id'=>$v->version_id
          	                     );
          	$datas[] = $v->category_id; 
       	}
       }
       $str = "(";
       if(count($datas)!=0){
	       foreach ($datas as $k=>$c){
	          $str.="'".$c."'";
	          if($k!=count($datas)-1){
	            $str.=",";
	          }
	       }
       }else{
          $str.="9999999";
       }
       $str.=")";
       $sql = "select * from category where category_id in ".$str;
       $sql.=" order by codes asc";
       if($num!=""){
         $sql.=" limit $offset,$num";
       }
       
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function main_select_chart($start,$end){

       $sql = "select * from category ";
       if($start!=''){
         $sql.= " where createtime between ".$start." and ".$end ;
       }
       $query = $this->db->query($sql);
       $result = $query->result();
       return $result;
    }
    
    
    function select($array,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($array!=""){
       	  $this->db->where($array);
       }
       $this->db->order_by('codes asc');
       $query = $this->db->get('category');
       
       $result = $query->result(); 
      // $this->db->close();
       return $result;
    }
    
    function select_export($mod_id){
        $this->db->select('mod.name as mod_name,category.*');
		$this->db->from('mod');
		$this->db->join('category', 'mod.mod_id = category.mod_id');
		$this->db->where(array('category.mod_id'=>$mod_id));
		$query = $this->db->get();
		$result = $query->result(); 
        return $result;
    }
    
    function update($id){
       $this->name = $_POST['name'];
       $this->codes = $_POST['codes'];
       $this->mod_id = $_POST['mod_id'];
       $this->level = $_POST['level'];
       $result = $this->db->update('category',$this,array('category_id'=>$id));
       return $result;
    }
    
    function update_array($id,$array){
       $result = $this->db->update('category',$array,array('category_id'=>$id));
       return $result;
    }
    
    function update_name($name,$array){
       $result = $this->db->update('category',$array,array('name'=>$name));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('category',array('category_id'=>$id));
       return $result;
    }
}